//路由鉴权:鉴权,项目当中路由能不能被的权限的设置(某一个路由什么条件下可以访问、什么条件下不可以访问)
import router from '@/router'
import { useUserStore } from './store/modules/user';
import { useCommonStore } from './store/modules/common';
// import nprogress from 'nprogress'
//引入进度条样式
// import 'nprogress/nprogress.css'
// nprogress.configure({ showSpinner: false })
//获取账户相关的小仓库内部token数据,去判断账户是否登录成功

//全局守卫:项目当中任意路由切换都会触发的钩子
//全局前置守卫
router.beforeEach(async (to:any, from:any, next:any) => {
  console.log('from =', from);
  console.log('to =', to);


  const commonStore = useCommonStore()
  commonStore.currentRouteUrl = to.path

  const userStore = useUserStore()
  // console.log('userStore.profile?.token =', userStore.profile?.token);


  // 刷新token
  await userStore.refreshToken(to.path)

  


  // ------------------ 已经登录了则不允许访问的页面包括【登录、注册、忘记密码页...】 ------------------
  const unRedirectUrls = [
    '/passwordLogin',
    '/messageLogin',
    '/register',
    '/forgotPassword',
  ]
  if (userStore.profile?.token && unRedirectUrls.includes(to.path)) {
    next(from.fullPath)
  }



  // ------------------------- 跳转的页面需要登录 -------------------------
  if (!userStore.profile?.token && to.meta.isLogin) {
    const redirectUrl = encodeURIComponent(to.fullPath)
    console.log('guard redirectUrl =', redirectUrl);
    next('/passwordLogin?redirect='+redirectUrl)
  }

  next()
})
//全局后置守卫
router.afterEach(() => {
  // nprogress.done()
})